package com.runen.infinitemovement.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.runen.infinitemovement.R;
import com.runen.infinitemovement.activity.MyHomeActivityV2;
import com.runen.infinitemovement.activity.MyTeamActivityV2;
import com.runen.infinitemovement.adapter.MemberAdapter;
import com.runen.infinitemovement.model.api.CompanyAPI;
import com.runen.infinitemovement.model.base.BaseResponseEntity;
import com.runen.infinitemovement.model.entity.MemberEntity;
import com.runen.infinitemovement.model.enums.ActivityCompat;
import com.runen.rxnetlibrary.net.callback.BaseResultCallback;
import com.runen.rxnetlibrary.utils.ToastUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Create by LiaoDuanHong
 * Create on
 */
public class MemberFragment extends Fragment {
    @BindView(R.id.rcv_content)
    RecyclerView rcv_content;
    @BindView(R.id.ll_tx)
    LinearLayout ll_tx;
    private View view;
    private MemberAdapter adapter;
    private MemberEntity.DataBean bean;

    public static Fragment newInstance() {
        Fragment fragment = new MemberFragment();
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (null == view) {
            view = inflater.inflate(R.layout.fragment_member, container, false);
            ButterKnife.bind(this, view);
        }
        return view;
    }

    public void initData() {
        bean = ((MyTeamActivityV2) getActivity()).entity;
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        rcv_content.setLayoutManager(layoutManager);
        adapter = new MemberAdapter(getActivity(), ((MyTeamActivityV2) getActivity()).leaderId);
        rcv_content.setAdapter(adapter);
//        bean.teamMemberList.add(0, bean.leader);
        List<MemberEntity.DataBean.TeamMemberListBean> teamMemberList = new ArrayList<>();
        teamMemberList.add(0, bean.leader);
        teamMemberList.addAll(bean.teamMemberList);

        ll_tx.setVisibility(((MyTeamActivityV2) getActivity()).isLeader ? View.VISIBLE : View.GONE);

        adapter.setNewInstance(teamMemberList);

        adapter.setOnClick(new MemberAdapter.OnClick() {
            @Override
            public void like(int position) {
                initLike(bean.team.companyId, teamMemberList.get(position).userId, 1, position);
            }

            @Override
            public void remind(int position) {
                initLike(bean.team.companyId, teamMemberList.get(position).userId, 2, position);
            }
        });
        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter1, @NonNull View view, int position) {
//                Intent intent = new Intent(getActivity(), MyHomeActivity.class);
                Intent intent = new Intent(getActivity(), MyHomeActivityV2.class);
                intent.putExtra(ActivityCompat.USER_ID, adapter.getData().get(position).userId);
                startActivity(intent);
            }
        });
    }

    public void initLike(String tenantId, String toUserId, int type, int position) {
        ((MyTeamActivityV2) getActivity()).showDialog();
        JSONObject object = new JSONObject();
        try {
            object.put("tenantId", tenantId);
            object.put("toUserId", toUserId);
            object.put("type", type);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        new CompanyAPI().teamLike(object.toString(), new BaseResultCallback<BaseResponseEntity<?>>() {
            @Override
            public void onResponse(BaseResponseEntity<?> response) {
                if (response.getCode() == 0) {
                    ((MyTeamActivityV2) getActivity()).dismissDialog();
                    if (type == 1) {
                        adapter.getData().get(position).isLikeStatus = true;
                        adapter.getData().get(position).likeNumber += 1;
                        adapter.notifyItemChanged(position);
                    } else {
                        adapter.getData().get(position).isRemindStatus = true;
                        adapter.getData().get(position).remindNumber += 1;
                        adapter.notifyItemChanged(position);
                    }
                    ((MyTeamActivityV2) getActivity()).dynamicFragment.initData();
                } else {
                    ToastUtils.showShort(response.getMessage());
                }
            }
        });
    }
}
